
# 导入 socket、sys 模块
import socket
import sys

# 创建 socket 对象
import threading

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 获取本地主机名
host = socket.gethostname()

# 设置端口号
port = 9999

# 连接服务，指定主机和端口
s.connect((host, port))

def send_message(s):
    while True:
        str = input("请输入：")

        s.send(str.encode())

        if str == "stop":
            s.close()
            break

send_thread = threading.Thread(target=send_message, args=(s,))  # 注意传入的参数一定是一个元组!
send_thread.start()

while True:
    # 接收小于 1024 字节的数据
    msg = s.recv(1024)

    print(msg.decode('utf-8'))
